#!/bin/sh
#
# renew -- clean a docker lab and renew it
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#

TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd)"
. "$TOP_DIR"/tools/docker/config >/dev/null

id_names=`docker ps -f status=running -f status=exited --format='{{.ID}},{{.Names}}' -q`

# Must rerun wproxy and tproxy too
info_print "Remove $WPROXY_NAME and $TPROXY_NAME"
docker rm -f $WPROXY_NAME $TPROXY_NAME >/dev/null 2>&1

for id_name in $id_names
do
    id=$(echo $id_name | cut -d ',' -f1)
    name=$(echo $id_name | cut -d ',' -f2)
 
    lab=`echo $name | sed -e "s%-[0-9]*-[0-9a-zA-Z]\{6,6\}$%%g"`
    get_labs | tr ' ' '\n' | grep -q ^$lab$
    [ $? -ne 0 ] && continue

    "$DOCKER_CLEAN_CMD" $lab

    "$DOCKER_RUN_CMD" $lab
done
